package com.myres.model;

import java.sql.Timestamp;
import com.myres.db.ConnectionPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;

public class Message {
	private int id;
	private Timestamp createTime;
	private int senderId;
	private int receiverId;
	private User sender;
	private User receiver;
    private int status;
    private String content;
    
    private String senderEmail;
    private String receiverEmail;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Timestamp getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}
	public int getSenderId() {
		return senderId;
	}
	public void setSenderId(int senderId) {
		this.senderId = senderId;
	}
	
	public int getReceiverId() {
		return receiverId;
	}
	
	public void setReceiverId(int receiverId) {
		this.receiverId = receiverId;
	}
	
	public User getSender(){
	    return this.sender;
	}
	
	public User getReceiver(){
	    return this.receiver;
	}
	
	public void setSender(User sender){
	    this.sender=sender;
		this.senderId = sender.getId();
	}
	
	public void setReceiver(User receiver){
	    this.receiver=receiver;
		this.receiverId = receiver.getId();
	}
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public String getSenderEmail() {
		return senderEmail;
	}
	public void setSenderEmail(String senderEmail) {
		this.senderEmail = senderEmail;
	}
	public String getReceiverEmail() {
		return receiverEmail;
	}
	public void setReceiverEmail(String receiverEmail) {
		this.receiverEmail = receiverEmail;
	}
	public int save(){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			PreparedStatement pstmt=con.prepareStatement("insert into res_messages(_sender,_receiver,_content,_status)values(?,?,?,?)");
			pstmt.setInt(1, this.senderId);
			pstmt.setInt(2, this.receiverId);
			pstmt.setString(3, this.content);
			pstmt.setInt(4, this.status);
			return pstmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
			return -1;
		}finally{
			try{
				con.close();
			}catch(Exception e){
				
			}
		}
	}
	public static List<Message> populate(ResultSet rs){
		List<Message> messages=new ArrayList<Message>();
		if(rs==null) return messages;
		try{
		while(rs.next()){
			Message message=new Message();
			message.setId(rs.getInt(1));
			message.setSenderId(rs.getInt(2));
			message.setReceiverId(rs.getInt(3));
			message.setCreateTime(rs.getTimestamp(4));
			message.setContent(rs.getString(5));
			message.setStatus(rs.getInt(6));
			messages.add(message);
		}
		return messages;
		}catch(SQLException e){
			e.printStackTrace();
			return messages;
		}
	}
}
